# -*- coding: utf-8 -*-
"""
@author:  yinbing.li(yinbing.li@zillnk.com)
@version: 1.0.0
@file:    leetcode5_最长回文子串_中心扩展法.py
@time:    2023/4/14 9:35
"""
class Solution:
    def longestPalindrome(self, s: str) -> str:
        ans = ""
        for i in range(len(s)):
            t = max(self.expand_from_center(i, i, s), self.expand_from_center(i, i+1, s))
            d, m = divmod(t, 2)
            if m == 1:
                left = i - t // 2
                right = i + t // 2
            else:
                left = i - (t - 1) // 2
                right = i + (t - 1) // 2 + 1
            if len(s[left: right+1]) > len(ans):
                ans = s[left: right+1]
        return ans

    def expand_from_center(self, left, right, s):
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return right - left - 1


s = Solution()
print(s.longestPalindrome("babad"))